package com.tour.service.impl;

import com.tour.bean.Consumer;
import com.tour.bean.ReturnT;
import com.tour.dao.ConsumerMapper;
import com.tour.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ConsumerServiceImpl
 * @Author 张梦兴
 * @Date 2020/3/13 17:20
 * @describe
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Autowired
    private ConsumerMapper consumerMapper;


    @Override
    public ReturnT<String> insert(Consumer consumer) {

        // valid
        if (consumer == null) {
            return new ReturnT<String>(ReturnT.FAIL_CODE, "必要参数缺失");
        }

        consumerMapper.insert(consumer);
        return ReturnT.SUCCESS;
    }


    @Override
    public ReturnT<String> delete(int id){
        int ret = consumerMapper.delete(id);
        return ret>0?ReturnT.SUCCESS:ReturnT.FAIL;
    }


    @Override
    public ReturnT<String> update(Consumer consumer){
        int ret = consumerMapper.update(consumer);
        return ret>0?ReturnT.SUCCESS:ReturnT.FAIL;
    }


    @Override
    public Consumer load(int id) {
        return consumerMapper.load(id);
    }

    @Override
    public Consumer login(String email, String password) {
        return consumerMapper.loadByEmailAndPassword(email, password);
    }

    @Override
    public ReturnT<String> loadByEmail(String email) {
        Consumer consumer = consumerMapper.loadByEmail(email);
        if(null==consumer){
            return ReturnT.SUCCESS;
        }
        return ReturnT.FAIL;
    }


    @Override
    public Map<String,Object> pageList(int offset, int pagesize) {

        List<Consumer> pageList = consumerMapper.pageList(offset, pagesize);
        int totalCount = consumerMapper.pageListCount(offset, pagesize);

        // result
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("pageList", pageList);
        result.put("totalCount", totalCount);

        return result;
    }
}
